package cn.com.wangkai.wangkaisiji;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.com.wangkai.wangkaisiji.utils.Conts;
import cn.com.wangkai.wangkaisiji.utils.Utils;

public class MainActivity extends BasetActivity {
    @ViewInject(R.id.et_username)
    EditText etusername;
    @ViewInject(R.id.et_password)
    EditText etpassword;
    @ViewInject(R.id.bt_login)
    Button btnlogin;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    x.view().inject(this);
    listener();
    }

    private void listener() {
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=etusername.getText().toString();
                String pwd=etpassword.getText().toString();
                if(TextUtils.isEmpty(name)||name.length()!=11){
                    Utils.toast(MainActivity.this,"请输入手机号");
                }else if(TextUtils.isEmpty(pwd)){
                    Utils.toast(MainActivity.this,"请输入密码");
                }else {
                    RequestParams params=new RequestParams(Conts.LOGIN_URL);
                    params.addBodyParameter("phone",name);
                    params.addBodyParameter("password",pwd );

                    x.http().post(params, new Callback.CommonCallback<JSONObject>() {
                        @Override
                        public void onSuccess(JSONObject result) {

                            try {
                                int code=result.getInt("code");
                                String token=result.getJSONObject("data").getString("token");
                                Log.i("token",result.toString());
                                if (code==1){
                                    Intent intent=new Intent(MainActivity.this,OrderActivity.class);
                                    intent.putExtra("token",token);
                                    startActivity(intent);
                                }else{
                                    Utils.toast(MainActivity.this,result.getString("message"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Utils.toast(MainActivity.this,"网络异常");
                            Log.i("login","login"+ex.toString());
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });

                }
            }
        });
    }

<<<<<<< HEAD
    private void load(String name,String pwd) {
        RequestParams params=new RequestParams(Conts.LOGIN_URL);
        params.addBodyParameter("name",name);
        params.addBodyParameter("pwd",pwd );

        x.http().post(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                try {
                    int code=result.getInt("code");
                    String token=result.getJSONObject("data").getString("token");
                    Log.i("token",token.toString());
                    if (code==1){
                        Intent intent=new Intent(MainActivity.this,OrderActivity.class);
                        intent.putExtra("token",token);
                        startActivity(intent);
                    }else{
                    String message=result.getString("message");
                        Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MainActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
=======
>>>>>>> 34b4534a310d20f67a83c2d44d2db9e85428da60





}
